package com.wei.btsearch;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.*;
import android.widget.*;
import com.wei.btsearch.components.HistoryAdaptor;
import com.wei.btsearch.components.ReadmeActivity;
import com.wei.btsearch.components.SearchResult;
import com.wei.btsearch.components.StartSearch;
import com.wei.btsearch.configurations.AppConfiguration;
import com.wei.btsearch.storage.DataBaseOperation;
import com.wei.btsearch.storage.HistoryItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener /*, HistoryAdaptor.OnItemOperate*/ {

    Toolbar toolbar;
    DrawerLayout drawer;
    NavigationView navigationView;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        randomSetTheme();
        setContentView(R.layout.activity_main);

        initViews();
    }


    private void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

//        gridView = (ListView) findViewById(R.id.history);

//        searchView = (SearchView) findViewById(R.id.search_view);
//        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            @Override
//            public boolean onQueryTextSubmit(String query) {
//                operation.insertContent(query);
//                Intent intent = new Intent(MainActivity.this, SearchResult.class);
//                intent.putExtra(AppConfiguration.SEARCH_CONTENT, query);
////                intent.putExtra(AppConfiguration.ENGINE_DEFAULT, sharedPreferences.getInt(AppConfiguration.ENGINE_DEFAULT, 0));
//                startActivity(intent);
//                return true;
//            }
//
//            @Override
//            public boolean onQueryTextChange(String newText) {
//                return false;
//            }
//        });

//        operation = new DataBaseOperation(MainActivity.this);
        sharedPreferences = getSharedPreferences(AppConfiguration.ENGINE_SHAREDPREFERENCE, 0);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.search_button) {
            startActivity(new Intent(this, StartSearch.class));
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    //    private void setHistoryList() {
//        history.clear();
//        Cursor cursor = operation.queryAll();
//        while (cursor.moveToNext()) {
//            history.add(new HistoryItem(cursor.getInt(0), cursor.getString(1)));
//        }
//
//        if (AppConfiguration.DEBUG) {
//            System.out.println(history);
//        }
//
//        gridView.setAdapter(new HistoryAdaptor(MainActivity.this, operation, history, MainActivity.this));
//
//    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
//         Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.settings) {
            final SharedPreferences preferences = sharedPreferences;

            final int[] engine = {preferences.getInt(AppConfiguration.ENGINE_DEFAULT, 0)};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("选择解析引擎")
                    .setCancelable(true)
                    .setSingleChoiceItems(R.array.engines, engine[0], new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            engine[0] = i;
                            if (AppConfiguration.DEBUG) {
                                System.out.println(engine[0] + "selected");
                            }
                        }
                    })
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        //从0开始计数
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt(AppConfiguration.ENGINE_DEFAULT, engine[0]);
                            editor.commit();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, null);

            builder.create().show();
        } else if (id == R.id.changetheme) {
//            randomSetTheme();
        } else if (id == R.id.readme) {
            startActivity(new Intent(this, ReadmeActivity.class));
        }
        return true;
    }

    private void randomSetTheme() {
        Random random = new Random();
        int type = random.nextInt(6);
        switch (type) {
            case 0:
                setTheme(R.style.AppTheme);
                break;
            case 1:
                setTheme(R.style.BlueTheme);
                break;
            case 2:
                setTheme(R.style.IndegoTheme);
                break;
            case 3:
                setTheme(R.style.LimeTheme);
                break;
            case 4:
                setTheme(R.style.PinkTheme);
                break;
            case 5:
                setTheme(R.style.PunpleTheme);
                break;
            case 6:
                setTheme(R.style.TealTheme);
                break;
        }
    }

}